/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package charlie.shop.warehouse;

import charlie.shop.misc.Common;
import com.charlie.db.DAOLocal;
import com.charlie.db.ShipmentOrderData;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Nabytovych Andrey
 */
@WebServlet(name = "ReportShipmentOrder", urlPatterns = {"/warehouse/shipmentOrder"})
public class ReportShipmentOrder extends HttpServlet {

    private final int ITEMS_PER_PAGE = 20;
    private boolean isOfFilterPage = false;
    private boolean[] status = new boolean[]
    {
        true, true, true, true
    };
    private String email = new String();
    
    @EJB
    DAOLocal dao;
    
    /**
     * Handles the GET HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        int page = Common.getPage(request);
        int from = (page - 1) * ITEMS_PER_PAGE + 1;
        int to = from - 1 + ITEMS_PER_PAGE;
        
        List<ShipmentOrderData> shipmentOrder = dao.getOrdersByFilter(null, status, from, to, -1, -1);
        
        request.setAttribute("shipmentOrderList", shipmentOrder);
        request.setAttribute("page", page);
        request.setAttribute("pagesCount", dao.getOrdersByFilter(null, status, -1, -1, -1, -1).size() / ITEMS_PER_PAGE);
        
        getServletContext().getRequestDispatcher("/warehouse/reportShipmentOrder.jsp").forward(request, response);
    }

    /**
     * Handles the POST HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=\"Report (shipment orders).xls\"");
        
        try {
            new WriteExcelUsedApachePOI().toFile(response.getOutputStream(), dao.getOrdersByFilter(null, null, -1, -1, -1, -1));
        } catch (Exception ex) {
            Logger.getLogger(ReportServlet.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}
